package com.zzt.zt_bigd;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.LanguageUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }


    private void initView() {
        String s = roundUp(4.4, 2);
        Log.d(TAG, ">>>> :" + s);

        String s1 = keep2DecimalPlaces(5.5, 2);
        Log.w(TAG, ">>>> 1: " + s1);

        findViewById(R.id.button).setOnClickListener(v ->
                LanguageUtils.applyLanguage(Locale.ITALIAN, false)
        );
        findViewById(R.id.button1).setOnClickListener(v ->
                LanguageUtils.applyLanguage(Locale.ENGLISH, false)
        );
        findViewById(R.id.button2).setOnClickListener(v ->
                LanguageUtils.applyLanguage(Locale.GERMAN, false)
        );
        findViewById(R.id.button3).setOnClickListener(v ->
                LanguageUtils.applyLanguage(Locale.CHINESE, false)
        );
    }

    public static String keep2DecimalPlaces(double value, int digit) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(digit);
        nf.setRoundingMode(RoundingMode.HALF_UP);
        return nf.format(value);
    }


    public static String roundUp(double d, int scale) {
        try {
            BigDecimal big = new BigDecimal(d);
            double res = big.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
            Log.d(TAG, "gggg:" + String.valueOf(res));
            return String.format("%." + scale + "f", res);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return d + "";
    }

}